package com.brillilab.service.core.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.user.UsersMapper;
import com.brillilab.domain.enums.lab.UserPayTypeEnum;
import com.brillilab.domain.po.user.Users;
import com.brillilab.domain.vo.lab.LabInfoVo;
import com.brillilab.domain.vo.user.UsersVo;
import com.brillilab.service.core.user.IUsersService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 用户服务
 *
 * @author wmh
 */
@Service
@Transactional
public class UsersServiceImpl implements IUsersService {

    @Resource
    private UsersMapper usersMapper;

    @Override
    public Users selectUser(Long userId, Integer state) {
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getId, userId);
        if (state != null) {
            wrapper.eq(Users::getState, state);
        }

        return usersMapper.selectOne(wrapper);
    }

    @Override
    public List<Users> selectUserList(List<Long> userIdList, Integer state) {
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Users::getId, userIdList);
        if (state != null) {
            wrapper.eq(Users::getState, state);
        }
        return usersMapper.selectList(wrapper);
    }

    @Override
    public Users selectUser(String mobile, String password, Integer state) {

        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getMobile, mobile);
        wrapper.eq(Users::getPassword, DigestUtils.md5Hex(password).toUpperCase());
        if (state != null) {
            wrapper.eq(Users::getState, state);
        }
        return usersMapper.selectOne(wrapper);
    }

    @Override
    public Users selectUser(String mobile, Integer state) {

        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getMobile, mobile);
        if (state != null) {
            wrapper.eq(Users::getState, state);
        }

        return usersMapper.selectOne(wrapper);
    }

    @Override
    public Users selectUser(String mobile, String securityPwd) {
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getMobile, mobile);
        wrapper.eq(Users::getSecurityPwd, DigestUtils.md5Hex(securityPwd).toUpperCase());
        wrapper.eq(Users::getState, 1);
        return usersMapper.selectOne(wrapper);
    }

    @Override
    public Users selectUser(Long labId, Long labMemberId, Integer state) {
        return usersMapper.selectUser(labId, labMemberId, state);
    }

    @Override
    public boolean updateById(Users me) {
        int i = usersMapper.updateById(me);
        return i == 1;
    }

    @Override
    public Users selectUserByEmail(String email, Integer state) {
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getEmail, email);
        wrapper.eq(Users::getState, state);
        List<Users> usersList = usersMapper.selectList(wrapper);
        return !CollectionUtils.isEmpty(usersList) ? usersList.get(0) : null;
    }

    @Override
    public boolean setLabIdNull(Long id) {
        return usersMapper.setLabIdNull(id) == 1;
    }

    @Override
    public List<LabInfoVo> selectLabInfoVo(Long id, Integer state) {
        return usersMapper.selectLabInfoVo(id, state);
    }

    @Override
    public Users insert(Users user) {
        usersMapper.insert(user);
        return user;
    }

    @Override
    public List<UsersVo> selectUserList(Long labId, List<Long> labMemberIds, Integer state) {
        return usersMapper.selectUserList(labId, labMemberIds, state);
    }

    @Override
    public int updateLastLoginTime(Long id) {
        Users update = new Users();
        update.setId(id);
        update.setLastLoginTime(new Date());
        return usersMapper.updateById(update);
    }

    @Override
    public List<Users> selectUserList(String email) {
        return usersMapper.selectList(new LambdaQueryWrapper<Users>().eq(Users::getEmail,email));
    }

    @Override
    public List<Users> selectAllUserList() {
        return usersMapper.selectList(new LambdaQueryWrapper<Users>().eq(Users::getState,1));
    }

    @Override
    public List<Users> selectAllTryUserList() {
        return usersMapper.selectList(new LambdaQueryWrapper<Users>().eq(Users::getState,1)
                .eq(Users::getPayType, UserPayTypeEnum.TRY.getValue()));
    }
}
